const mysql = require("mysql");

const pool = mysql.createPool({
    host: "127.0.0.1",
    user: "root",
    password: "root",
    database: "cs2107"
})

const exec = (sql, params) => {
    return new Promise((resolve, reject) => {
        pool.getConnection(function (err, conn) {
            if (err) {
                console.log("连接数据库失败:" + err.message);
                reject(err.message);
            }
            conn.query(sql, params, (err, results) => {
                conn.release()
                if (err) {
                    console.log("执行sql语句失败:" + err.message);
                    reject(err.message);
                }
                resolve(results)
            })
        })
    })
}

module.exports = { exec };